<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./react/react17.development.js"></script>
    <script src="./react/react-dom17.development.js"></script>
    <script src="./react/babel.min.js"></script>
</head>

<body>
    <div id="main"></div>
    <script type="text/babel">
        async function ajax(url) {
            return fetch(url, { method: "get" })
                .then((data) => data.json())
                .then((res) => res.data);
        }
        class Com extends React.Component {
            constructor() {
                super();
                this.state = {
                    dataList: []
                }
            }
            render() {
                // console.log("render被调用了");
                let { dataList } = this.state
                return (<div>
                    <ul>
                        {dataList.map((item, index) => (<li key={index}>
                            <img src={item.author.avatar_url} alt="" />
                            <h2>{item.title}</h2>
                        </li>))}
                    </ul>
                </div>)
            }
            // componentDidMount() 声明周期函数中的一个
            // 渲染dom时执行的函数
            //  组件加载时直接执行的代码，放在这个函数内
            async componentDidMount() {
                console.log("componentDidMount被调用了");
                let res = await ajax("https://cnodejs.org/api/v1/topics")
                console.log(res);
                this.setState({
                    dataList: res
                })
            }
        }
        ReactDOM.render(<Com />, document.getElementById("main"))
    </script>
</body>

</html>